from selenium import webdriver
import time
import pickle
import json

options = webdriver.ChromeOptions()
options.add_argument('lang=zh_CN.UTF-8')

driver = webdriver.Chrome()

# 首次登录时获取并保存cookie
def save_cookie():
    driver.get("https://debug.baidu-int.com/")
    # 手动登录操作
    time.sleep(30)  # 等待20秒，期间手动登录网站
    # 获取并保存cookie
    with open("cookies.pkl", "wb") as f:
        pickle.dump(driver.get_cookies(), f)
    print(json.dumps(driver.get_cookies(), ensure_ascii=False, indent=4))

# 使用保存的cookie登录
def load_cookie():
    # 加载cookie
    driver.set_page_load_timeout(3) # 设置页面加载超时时间
    try:
        driver.get("https://debug.baidu-int.com/")
    except:
        pass
    with open("cookies.pkl", "rb") as f:
        cookies = pickle.load(f)
        for cookie in cookies:
            driver.add_cookie(cookie)
    driver.get("https://debug.baidu-int.com/")
    time.sleep(20)
    driver.refresh()  # 刷新页面

#save_cookie()
load_cookie()
